home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / c / list.c < prev    next >
C/C++ Source or Header  |  1996-09-13  |  4KB  |  177 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: list.c,v 1.5 1996/09/13 17:52:10 digulla Exp $
  4.     $Log: list.c,v $
  5.     Revision 1.5  1996/09/13 17:52:10  digulla
  6.     Use IPTR
  7.  
  8.     Revision 1.4  1996/09/12 14:49:25  digulla
  9.     More checks
  10.     Better code to specify size of buffer
  11.  
  12.     Revision 1.3  1996/08/01 17:40:45  digulla
  13.     Added standard header for all files
  14.  
  15.     Desc:
  16.     Lang:
  17. */
  18. #include <exec/memory.h>
  19. #include <clib/exec_protos.h>
  20. #include <dos/dos.h>
  21. #include <dos/exall.h>
  22. #include <dos/datetime.h>
  23. #include <clib/dos_protos.h>
  24. #include <utility/tagitem.h>
  25.  
  26. /* Don't define symbols before the entry point. */
  27. extern struct ExecBase *SysBase;
  28. extern struct DosLibrary *DOSBase;
  29. extern const char dosname[];
  30. static LONG tinymain(void);
  31.  
  32. __AROS_LH0(LONG,entry,struct ExecBase *,sysbase,,)
  33. {
  34.     __AROS_FUNC_INIT
  35.     LONG error=RETURN_FAIL;
  36.  
  37.     SysBase=sysbase;
  38.     DOSBase=(struct DosLibrary *)OpenLibrary((STRPTR)dosname,39);
  39.     if(DOSBase!=NULL)
  40.     {
  41.     error=tinymain();
  42.     CloseLibrary((struct Library *)DOSBase);
  43.     }
  44.     return error;
  45.     __AROS_FUNC_EXIT
  46. }
  47.  
  48. struct ExecBase *SysBase;
  49. struct DosLibrary *DOSBase;
  50. const char dosname[]="dos.library";
  51.  
  52. static LONG tinymain(void)
  53. {
  54.     char *args[1]={ 0 };
  55.     struct RDArgs *rda;
  56.     BPTR dir;
  57.     LONG i;
  58.     LONG loop;
  59.     struct ExAllControl *eac;
  60.     struct ExAllData *ead;
  61.     static UBYTE buffer[4096];
  62.     UBYTE flags[9];
  63.     IPTR argv[5];
  64.     LONG error=0, rc=RETURN_OK;
  65.  
  66.     rda=ReadArgs("DIR",(IPTR *)args,NULL);
  67.     if(rda!=NULL)
  68.     {
  69.     dir=Lock(args[0]!=NULL?args[0]:"",SHARED_LOCK);
  70.     if(dir)
  71.     {
  72.         LONG files=0, dirs=0, blocks=0;
  73.         eac=AllocDosObject(DOS_EXALLCONTROL,NULL);
  74.         if(eac!=NULL)
  75.         {
  76.         eac->eac_LastKey=0;
  77.         do
  78.         {
  79.             loop=ExAll(dir,(struct ExAllData *)buffer,sizeof(buffer),ED_COMMENT,eac);
  80.             if(!loop&&IoErr()!=ERROR_NO_MORE_ENTRIES)
  81.             {
  82.             error=RETURN_ERROR;
  83.             break;
  84.             }
  85.             if(eac->eac_Entries)
  86.             {
  87.             ead=(struct ExAllData *)buffer;
  88.             do
  89.             {
  90.                 UBYTE date[LEN_DATSTRING];
  91.                 UBYTE time[LEN_DATSTRING];
  92.                 struct DateTime dt;
  93.                 dt.dat_Stamp.ds_Days  =ead->ed_Days;
  94.                 dt.dat_Stamp.ds_Minute=ead->ed_Mins;
  95.                 dt.dat_Stamp.ds_Tick  =ead->ed_Ticks;
  96.                 dt.dat_Format =FORMAT_DOS;
  97.                 dt.dat_Flags  =DTF_SUBST;
  98.                 dt.dat_StrDay =NULL;
  99.                 dt.dat_StrDate=date;
  100.                 dt.dat_StrTime=time;
  101.                 DateToStr(&dt); /* returns 0 if invalid */
  102.                 ead->ed_Prot^=0xf;
  103.                 for(i=0;i<7;i++)
  104.                 if(ead->ed_Prot&(64>>i))
  105.                     flags[i]="sparwed"[i];
  106.                 else
  107.                     flags[i]='-';
  108.  
  109.                 flags[i] = 0;
  110.  
  111.                 argv[0]=(IPTR)ead->ed_Name;
  112.                 if(ead->ed_Type>=0)
  113.                 {
  114.                 argv[1]=(IPTR)flags;
  115.                 argv[2]=(IPTR)date;
  116.                 argv[3]=(IPTR)time;
  117.                 if(VPrintf("%-25.s   <Dir> %7.s %s %s\n",argv)<0)
  118.                 {
  119.                     error=RETURN_ERROR;
  120.                     loop=0;
  121.                     break;
  122.                 }
  123.                 dirs++;
  124.                 }else
  125.                 {
  126.                 argv[1]=ead->ed_Size;
  127.                 argv[2]=(IPTR)flags;
  128.                 argv[3]=(IPTR)date;
  129.                 argv[4]=(IPTR)time;
  130.                 if(VPrintf("%-25.s %7.ld %7.s %s %s\n",argv)<0)
  131.                 {
  132.                     error=RETURN_ERROR;
  133.                     loop=0;
  134.                     break;
  135.                 }
  136.                 blocks+=ead->ed_Size;
  137.                 files++;
  138.                 }
  139.                 ead=ead->ed_Next;
  140.             }while(ead!=NULL);
  141.             }
  142.         }while(loop);
  143.         if(!error)
  144.         {
  145.             argv[0]=files;
  146.             argv[1]=dirs;
  147.             argv[2]=blocks;
  148.             if(VPrintf("%ld files - %ld directories - %ld bytes used\n",argv)<0)
  149.             error=RETURN_ERROR;
  150.         }
  151.         FreeDosObject(DOS_EXALLCONTROL,eac);
  152.         }else
  153.         {
  154.         SetIoErr(ERROR_NO_FREE_STORE);
  155.         error=RETURN_ERROR;
  156.         }
  157.         UnLock(dir);
  158.     }
  159.     else
  160.     {
  161.         PrintFault(IoErr(),"List: Lock failed");
  162.         rc = RETURN_ERROR;
  163.     }
  164.  
  165.     FreeArgs(rda);
  166.     }
  167.     else
  168.     {
  169.     error=RETURN_FAIL;
  170.     }
  171.  
  172.     if (error)
  173.     PrintFault(IoErr(),"List");
  174.  
  175.     return rc;
  176. }
  177.